using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SurveyHost.Core;

namespace SurveyHost.Web.Participant
{
    public partial class ParticipantValidation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(User.IsInRole("Admin")) return;
            Guid userId = WebUtil.CurrentUserId();
            if (StatusHelper.IsCompleted(userId) && !StatusHelper.UserIsValid(userId))
                Response.Redirect("NonParticipating.aspx", true);
            if (StatusHelper.UserIsValid(userId))
                Response.Redirect("Survey.aspx", true);
            // user is not valid, but may have yet to complete validation
            if (StatusHelper.UserHasCompletedValidation(userId))
                Response.Redirect("SurveyComplete.aspx", true);
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            if (!IsValid || User.IsInRole("Admin")) return;
            StatusHelper.SetValidationStatus(
                WebUtil.CurrentUserId(),
                AgeButtonList.SelectedIndex == 1,
                RelationshipButtonList.SelectedIndex == 1);

            if (AgeButtonList.SelectedIndex == 1 && RelationshipButtonList.SelectedIndex == 1)
                Response.Redirect("Survey.aspx", true);
            else
            {
                // mark the user complete so they can't take the survey
                SurveyManager.SessionInstance.CompleteSurvey();
                Response.Redirect("SurveyComplete.aspx", true);
            }
        }
    }
}
